var Define = require("../../../lib/Define.js")

/**
 * 跳转至评论页面
 * data.ofTb    指定表
 * data.ofId    指定行
 */
module.exports.onCommentBtnClick = function(data) {

  if(!data){
    data = {}
  }

  var params = ""
  if(data.ofTb){
    params = params+"?ofTb="+data.ofTb
  }
  if(data.ofId){
    params = params + "&ofId="+data.ofId
  }

  wx.navigateTo({
    url: '/pages/comment/sendComment'+params
  })
}

/**
 * 发送评论
 * data.comment 评论正文
 * data.ofTb    指定表
 * data.ofId    指定行
 * data.replyUserName 回复人的名字 nullable
 * data.replyUserId   回复人的id  nullable
 */
module.exports.postComment = function(data,callback){

  if(!data.comment){
    return
  }
  if(!data.ofTb){
    return
  }
  if(!data.ofId){
    return
  }
  
  data.bid = Define.value.bid

  wx.showLoading({
    title: '加载中...',
    mask: true
  })

  var userInfo = Define.getStoredUserInfo()
  if(userInfo){
    if(userInfo.nickName){
      data.fromUserName = userInfo.nickName
    }
    if(userInfo.id){
      data.fromUserId = userInfo.id
    }
  }

  wx.request({
    url: Define.value.baseUrl + '/app/comment/add',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    data: data,
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    success: function (res) {
      if(res.data.code == 200){
        callback(true);
      }else{
        callback(false);
      }
    },
    fail: function () {
      // fail
      console.log("失败")
      callback(false)
    },
    complete: function () {
      // complete

      wx.hideLoading()

    }
  })
}

/**
 * 列表评论
 * ofTb    指定表
 * ofId    指定行
 */
module.exports.listComment = function(ofId,ofTb,callback){

  var data = {}

  data.bid = Define.value.bid
  data.ofId = ofId
  data.ofTb = ofTb

  wx.showLoading({
    title: '加载中...',
    mask:true
  })

  wx.request({
    url: Define.value.baseUrl+'/app/comment/list',
    data:data,
    method:"GET",
    success:function(res){
      var list = []
      if(res.data.code == 200){
        list = res.data.data
      }

      callback(list)
    },
    fail:function(){
      callback([])
    },
    complete:function(){
      wx.hideLoading()
    }
  })
}

/**
 * 获取点赞数
 * ofTb    指定表
 * ofId    指定行
 */
module.exports.likeCount = function(ofId,ofTb,callback){

  var data = {}

  data.bid = Define.value.bid
  data.ofId = ofId
  data.ofTb = ofTb

  wx.request({
    url: Define.value.baseUrl + '/app/comment/likecount',
    data: data,
    method: "GET",
    success: function (res) {
      var count = 0
      if (res.data.code == 200) {
        count = res.data.data
      }

      callback(count)
    },
    fail: function () {
      callback(0)
    }
  })
}

/**
 * 点赞
 * ofTb    指定表
 * ofId    指定行
 */
module.exports.like = function (ofId, ofTb, callback) {

  var data = {}

  data.bid = Define.value.bid
  data.ofId = ofId
  data.ofTb = ofTb

  wx.request({
    url: Define.value.baseUrl + '/app/comment/like',
    data: data,
    method: "GET",
    success: function (res) {
      var count = 0
      if (res.data.code == 200) {
        count = res.data.data
      }

      callback(count)
    },
    fail: function () {
      callback(0)
    }
  })
}

/**
 * 获取浏览数
 * ofTb    指定表
 * ofId    指定行
 */
module.exports.viewCount = function (ofId, ofTb, callback) {

  var data = {}

  data.bid = Define.value.bid
  data.ofId = ofId
  data.ofTb = ofTb

  wx.request({
    url: Define.value.baseUrl + '/app/comment/viewcount',
    data: data,
    method: "GET",
    success: function (res) {
      var count = 0
      if (res.data.code == 200) {
        count = res.data.data
      }

      callback(count)
    },
    fail: function () {
      callback(0)
    }
  })
}